/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.youth.fund.hk.hostManager.common.dao.ICommonDao;
import com.youth.fund.hk.hostManager.common.service.impl.CommonService;
import com.youth.fund.hk.hostManager.manager.dao.TypeDao;
import com.youth.fund.hk.hostManager.manager.datamodel.TypeModel;
import com.youth.fund.hk.hostManager.manager.service.InitService;
import com.youth.fund.hk.hostManager.manager.service.TypeService;

/**
 * @author Sunk_Sky
 *
 * 2014年11月19日下午3:06:21
 */
@Service("TypeService")
public class TypeServiceImpl extends CommonService<TypeModel> implements
		TypeService
{
	private TypeDao dao;
	
	@Autowired
	@Qualifier("InitService")
	private InitService initService;
	
	@Autowired
	@Qualifier("TypeDao")
	@Override
	public void setCommonDao(ICommonDao<TypeModel> commonDao)
	{
		this.commonDao = commonDao;
		
		this.dao = (TypeDao) commonDao;

	}
	public TypeModel getParent(TypeModel model)
	{
		if(model.getParent()!=null)
		{
			return model.getParent();
		}
		
		TypeModel tm = this.commonDao.queryOne(model);
		
		int parent_id;
		
		String parent_code;
		if(tm !=null)
		{
			parent_id = tm.getParent_id();
			
			parent_code = tm.getParent_code();
			
			/**
			 * 
			 * root是index的父亲，但是root是不存在的，也就是id=0
			 * 所有的目录的根目录是index
			 */
		
			if(parent_id == 0||"root".equalsIgnoreCase(parent_code))
			{
				return null;
			}
			
			TypeModel condition = new TypeModel();
			
			condition.setParent_id(parent_id);
			
			condition.setParent_code(parent_code);
			
			TypeModel parent = this.commonDao.queryOne(condition);
			
			model.setParent(parent);
			
			return parent;
		}
		
		
		return null;
	}


	public List<TypeModel> getChildren(TypeModel model)
	{
		if(model.getChildren_list()!=null)
		{
			return model.getChildren_list();
		}
		
		int id = model.getId();
				
		TypeModel condition = new TypeModel();
		
		condition.setParent_id(id);
		
		condition.setLang(model.getLang());
		
		List<TypeModel> list = dao.query(condition);
		
		
		return list;
	}
	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.service.impl.CommonService#generateHtml(java.io.Serializable)
	 */
	@Override
	public void generateHtml(TypeModel model)
	{
//		this.initService.initAll();
		
	}
	
	@Override
	public boolean delete(int id,String lang)
	{
		boolean result = false;
		
		TypeModel condition = new TypeModel();
		
		condition.setId(id);
		
		condition.setLang(lang);
		
		condition = dao.queryOne(condition);
		
		result = dao.delete(condition);
		
		if(!result)
		{
			return result;
		}
		
		List<TypeModel> children = this.getChildren(condition);
		
		for(TypeModel tm:children)
		{
			result = this.dao.delete(tm);
			
			if(!result)
			{
				return result;
			}
		}
		
		return result;
	}




}
